<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>授权确认</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }

    body {
      background-color: #520a18;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      padding: 20px;
    }

    .auth-container {
      background-color: white;
      border-radius: 12px;
      box-shadow: 0 4px 20px rgba(0,0,0,0.08);
      width: 100%;
      max-width: 500px;
      overflow: hidden;
    }

    .auth-header {
      background-color: #0052cc;
      color: white;
      padding: 20px;
      text-align: center;
    }

    .auth-header h1 {
      font-size: 22px;
      margin-bottom: 5px;
    }

    .auth-header p {
      font-size: 14px;
      opacity: 0.9;
    }

    .auth-body {
      padding: 30px;
    }

    .info-section {
      margin-bottom: 25px;
    }


    .app-logo img {
      max-width: 32px;
      max-height: 32px;
    }

    .info-details h3 {
      font-size: 16px;
      color: #172b4d;
      margin-bottom: 3px;
    }

    .info-details p {
      font-size: 14px;
      color: #6b7785;
    }

    .security-note {
      font-size: 13px;
      color: #6b7785;
      text-align: center;
      padding: 15px;
      background-color: #f9fafb;
      border-radius: 8px;
    }
  </style>
</head>
<body>
<div class="auth-container">
  <div class="auth-header">
    <h1>授权中 ...</h1>
    <p>即将转跳至应用</p>
  </div>
  <div class="auth-body">

<!--    <div class="info-section">-->
<!--      -->
<!--    </div>-->

    <div class="security-note">
      安全提示：仅授权您信任的应用。不会以任何理由要求您提供密码。
    </div>
  </div>
</div>
<script>

  function setCookie(name, value) {
    const date = new Date();
    date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
    const expires = `expires=${date.toUTCString()}`;
    document.cookie = `${name}=${encodeURIComponent(value)}; ${expires}; path=/`;
  }


  const searchParams = new URLSearchParams(window.location.search);

  // 返回指定参数的值，如果不存在则返回null
  const code =  searchParams.get("code");

  // 定义请求URL和参数
  // const url = '/sso/api/accessToken?code='+code;
  const url = './api/sso/accessToken?code='+code;

  // 创建XMLHttpRequest对象
  const xhr = new XMLHttpRequest();

  // 配置请求（GET方法，URL，异步）
  xhr.open('GET', url, true);

  // 设置响应类型为JSON
  xhr.responseType = 'json';

  // 监听请求完成事件
  xhr.onload = function() {
    if (xhr.status >= 200 && xhr.status < 300) {
      // 请求成功，获取响应数据中的data字段
      const responseData = xhr.response;
      const data = responseData.data;
      if (responseData.code !== 200) {
        alert(responseData.msg)
        return;
      }


      console.log(data)

      for (let dataKey in data.authList) {
        let datum = data.authList[dataKey];

        switch (datum.type) {
          case "localStorage":
            localStorage.setItem(datum.authKey,datum.authValPath);
            break;
          case "sessionStorage":
            sessionStorage.setItem(datum.authKey, datum.authValPath);
            break;
          case "cookies":
            setCookie(datum.authKey, datum.authValPath);
            break;
          case "IndexedDB":
            alert("暂不支持DB授权")
            break;
        }
      }

      setTimeout(() => {
        location.href=data.redirect;
      }, 10);


    } else {
      // 请求失败，处理错误
      console.error('请求失败，状态码:', xhr.status);
    }
  };

  // 监听网络错误
  xhr.onerror = function() {
    console.error('网络错误，无法连接到服务器');
  };

  // 发送请求
  xhr.send();
</script>
</body>
</html>
